package com.gcb.socket;

import java.io.*;
import java.net.*;
import java.io.FileOutputStream;
import java.text.*;
import java.util.*;
import javax.swing.*;
//import java.io.InputStreamReader.*;

/**
 * Created by IntelliJ IDEA.
 * User: chendt
 * Date: 2008-7-30
 * Time: 16:07:18
 * To change this template use File | Settings | File Templates.
 */
public class ServerThread extends Thread
{
  BufferedReader in;
  PrintWriter out;
  int Threadnum;
  File file;
  String time;
  public FileOutputStream dout=null;
  Serve ser;
  public String name="";
  Socket income;
  String starttime="";
  public ServerThread(Socket incoming,int i,Serve serve)
  {
    ser=serve;
    income=incoming;
    file=new File(".","Receive.txt");
    try
    {
      in=new BufferedReader(new InputStreamReader(incoming.getInputStream()));
      out=new PrintWriter(incoming.getOutputStream(),true);//用于回报数据
      dout = new FileOutputStream(file,true);    //true表示每次从文件尾写消息
    }catch(IOException e){}
    Threadnum=i;  //记录线程的序号
    SimpleDateFormat m=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    starttime=m.format(new Date());    //记录下开始时间
    
  }
  private void checkInput()  throws IOException
  {
    String line=null;
    if((line=in.readLine())!=null) //读到数据后 准备写入文件
    {
      if(name.equals(""))
      {
        name=line;
        this.setName(name);
      }
      else
      {
          out.println(line+"记录成功");//回报消息
          SimpleDateFormat m=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
          line=m.format(new Date())+" "+name+" "+line;
          //System.out.println(line);
          //System.out.println(Threadnum);
          ser.AddMessage(line);
          try
          {
            dout = new FileOutputStream(file,true);    //true表示每次从文件尾写消息
            line=line+"\r\n";  //window换行符
            dout.write(line.getBytes());
          }catch(IOException ioe){};
      }
    }
    try
    {
      income.sendUrgentData(0xFF);
    }catch(IOException ex){
      //已经断开连接
      String str;
      SimpleDateFormat m=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
      str=m.format(new Date())+" "+name+" 断开连接,持续时间为:"+getlasttime();
      ser.AddMessage(str);
      stop();   //结束线程!  //也许应该修改

    }
  }
  public void run()
  {
    try
    {
      while(true)
      {
        checkInput();
        sleep(1000);
      }
    } catch(InterruptedException ex){}
    catch(IOException ex){}
  }
  public String getlasttime()
  {

    SimpleDateFormat m=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    String nowtime=m.format(new Date());
    long startT=fromDateStringToLong(starttime);
    long endT=fromDateStringToLong(nowtime);
    long ss=(endT-startT)/(1000); //共计秒数
    int MM = (int)(ss%3600)/60;//分钟数
    int hh=(int)ss/3600; //小时数
    int second=(int)ss%60; //秒数
    time=hh+":"+MM+":"+second;
    return time;
  }
  public long fromDateStringToLong(String inVal)
  { //此方法计算时间毫秒
    Date date = null; //定义时间类型
    SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    try
    {
    date = inputFormat.parse(inVal); //将字符型转换成日期型
    } catch (Exception e) {
    e.printStackTrace();
    }
    return date.getTime(); //返回毫秒数
  }
  public String getIP()
  {
    String ip;
    ip=income.getInetAddress().toString() ;
    return ip;
  }
}
